﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.ExceptionHandler;
using GC2.Entities.Admin;
using GC2.Bussines.BMAdmin;

public partial class UserControls_Rubros : GenericUserControl
{
    #region Propiedades

    public DropDownList CboRubro
    {
        get { return cboRubro; }
        set { cboRubro = value; }
    }

    public DropDownList CboSubRubro
    {
        get { return cboSubRubro; }
        set { cboSubRubro = value; }
    }

    #endregion

    #region Controles

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SetearCultura();
            SetearConexion();
            if (!IsPostBack)
            {
                CargarRubros();
                CargarSubRubros();
                ListaArticulos();
            }
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region cboRubro_SelectedIndexChanged
    protected void cboRubro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            CargarSubRubros();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region cboSubRubro_SelectedIndexChanged
    protected void cboSubRubro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            ListaArticulos();
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion

    #region Metodos

    #region CargarRubros
    protected void CargarRubros()
    {
        try
        {
            List<Rubros> listaRubros = new List<Rubros>();
            BMRubros bmRubros = new BMRubros(Connection);
            Rubros rubro = new Rubros();
            listaRubros = bmRubros.BMRubrosSearch(rubro);
            foreach (Rubros itemR in listaRubros)
            {
                ListItem item = new ListItem();
                item.Value = itemR.Id_Rubro.ToString();
                item.Text = itemR.Rubro;
                cboRubro.Items.Add(item);
            }
            Session["listaRubros"] = listaRubros;
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region CargarSubRubros
    protected void CargarSubRubros()
    {
        try
        {
            List<SubRubros> listaSubRubros = new List<SubRubros>();
            BMSubRubros bmSubRubros = new BMSubRubros(Connection);
            SubRubros subRubro = new SubRubros();
            subRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            listaSubRubros = bmSubRubros.BMSubRubrosSearch(subRubro);
            cboSubRubro.Items.Clear();
            foreach (SubRubros itemSR in listaSubRubros)
            {
                ListItem item = new ListItem();
                item.Value = itemSR.Id_SubRubro.ToString();
                item.Text = itemSR.SubRubro;
                cboSubRubro.Items.Add(item);
            }
            Session["listaSubRubros"] = listaSubRubros;
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #region ListaArticulos
    protected void ListaArticulos()
    {
        try
        {
            List<Stock> listaArticulos = new List<Stock>();
            BMStock bmStock = new BMStock(Connection);
            Stock articulo = new Stock();
            articulo.Id_Rubro = Convert.ToInt16(CboRubro.SelectedValue);
            if (CboSubRubro.SelectedValue != "")
            {  
                articulo.Id_SubRubro = Convert.ToInt16(CboSubRubro.SelectedValue);
            }
            listaArticulos = bmStock.BMStockGetAll(articulo);
            Session["listaArticulos"] = listaArticulos;
            SubRubros subRubro = new SubRubros();
            subRubro.Id_Rubro = Convert.ToInt16(cboRubro.SelectedValue);
            subRubro.Rubro = cboRubro.SelectedItem.Text;
            if (CboSubRubro.SelectedValue != "")
            {
                subRubro.Id_SubRubro = Convert.ToInt16(cboSubRubro.SelectedValue);
                subRubro.SubRubro = cboSubRubro.SelectedItem.Text;
            }
            Session["SubRubro"] = subRubro;
        }
        catch (Exception ex) { throw ex; }
    }
    #endregion

    #endregion
}
